/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schoolLink;

import java.util.Vector;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Admin
 */
public class RMSManager {
    private Function function;
    
    
    public Vector newsNew       = new Vector();
    public Vector newsFavorite  = new Vector();
    
    public boolean flag_autoDelete = true;
    
    public RMSManager(Function f){
        function = f;
        readNews();
        readFavorite();
    }
    /**
     * 
     */
    private void readNews() {
        //throw new UnsupportedOperationException("Not yet implemented");
        try {
            RecordStore rs = RecordStore.openRecordStore(Constant.RMW_NEWS, true);
            if (rs.getNumRecords() != 0) {
                if (rs.getRecord(1) != null){
                    String sTemp = new String(rs.getRecord(1));
                    String[] a = function.splitString(sTemp, ""+Constant.RMW_NEWS_SPLITCHAR);
                    if (a.length>=1){                       
                        for (int i=0; i< a.length; i++){
                            if (a[i].length()>=1){
                                newsNew.addElement(new News(a[i].charAt(0), a[i].substring(1)));
                            }
                        }
                    }
                }
            }
            rs.closeRecordStore();
        } catch(RecordStoreException re) {

        }  
    }   
    /**
     * Kiem tra xem co nap them tiin nhan dc khong
     * @return 
     */
    public boolean checkforLoadNews(){
        int cNewNews = 0;
        for (int i=0; i<newsNew.size();i++){
            if (((News)newsNew.elementAt(i)).state==Constant.RMW_NEWS_CHAR_NEW){
                cNewNews++;
            }
        }
        
        if(cNewNews>=Constant.NEWS_MAX_UNREAD) {
            return false;
        }
        else{
            return true;
        }
    }
    
    /**
     * Kiem tra so tin da doc, neu qua so cho phep thi tu dong xoa
     * @return 
     */
    public boolean checkReadNews(){
        int count = 0;
        for (int i=0; i<newsNew.size();i++){
            if (((News)newsNew.elementAt(i)).state==Constant.RMW_NEWS_CHAR_NEW ){
                
            }else{
                count++;
            }
        }
        
        if(count>=Constant.NEWS_MAX_READED) {
            //xoa bot tin nhan da doc

            int size = newsNew.size();
            int pos = newsNew.size() - 1;

            while ((newsNew.size() >= size) && (pos > 0)) {
                if (((News) newsNew.elementAt(pos)).state == Constant.RMW_NEWS_CHAR_READ) {
                    newsNew.removeElementAt(pos);
                } else {
                    pos--;
                }
            }
        }
        return true;
    }
    
    /**
     * Them tin nhan moi vao ds
     */
    public boolean addFavoriteNews(News newitem) {
        if(newsFavorite.size()>=Constant.NEWS_MAX_FAVORITE){
            return false;
        }else{
            newsFavorite.insertElementAt(newitem, 0);
            return true;
        }       
    }
    
    /**
     * Tem tin nhan moi vao ds
     */
    public boolean addNews(News newitem){
//        int cReadedNews = 0;
//        int cNewNews = 0;
//        for (int i=0; i<newsNew.size();i++){
//            if (((News)newsNew.elementAt(i)).state==Constant.RMW_NEWS_CHAR_NEW){
//                cNewNews++;
//            }else{
//                cReadedNews++;
//            }
//        }
//        
//        if (cNewNews>=Constant.NEWS_MAX_UNREAD){
//            return false;
//        }
            
        newsNew.insertElementAt(newitem, 0);
        
        
//        if (cReadedNews>=Constant.NEWS_MAX_READED){
//            //xoa bot tin nhan da doc
//             
//             int size = newsNew.size();
//             int pos = newsNew.size()-1;
//             
//             while ((newsNew.size()>=size)&& (pos>0)){
//                 if (((News)newsNew.elementAt(pos)).state==Constant.RMW_NEWS_CHAR_READ){
//                     newsNew.removeElementAt(pos);
//                 }else {
//                     pos--;
//                 }
//             }
//        }
        
        
        
        return false;
    }
    
    /**
     * 
     */
    private void readFavorite() {
        //throw new UnsupportedOperationException("Not yet implemented");
                try {
            RecordStore rs = RecordStore.openRecordStore(Constant.RMW_NEWS_FAVORITE, true);
            if (rs.getNumRecords() != 0) {
                if (rs.getRecord(1) != null){
                    String sTemp = new String(rs.getRecord(1));
                    System.out.println(sTemp);
                    String[] a = function.splitString(sTemp.trim(), ""+Constant.RMW_NEWS_SPLITCHAR);
                    if (a.length>=1){                       
                        for (int i=0; i< a.length; i++){
                            if (a[i].length()>=1){
                                newsFavorite.addElement(new News(a[i].charAt(0), a[i].substring(1)));
                            }
                        }
                    }
                }
            }
            rs.closeRecordStore();
        } catch(RecordStoreException re) {

        } 
    }
    /**
     * Luu tat ca truoc khi thoat
     */
    public void onClose(){
        saveNews();
        saveFavorite();
    }
    /**
     * Luu tin tuc
     * @return 
     */
    private boolean saveNews() {
        if (newsNew.size() > 0) {
            String s = "";
            for (int i=0; i<newsNew.size(); i++){
                s+= ""+Constant.RMW_NEWS_SPLITCHAR +((News)newsNew.elementAt(i)).state + ((News)newsNew.elementAt(i)).content;
            }
            if (s.charAt(0)==Constant.RMW_NEWS_SPLITCHAR) {
                s=s.substring(1);
                
            }  
            System.out.println("RMS save:"+ s);
            try {
                RecordStore rs = RecordStore.openRecordStore(Constant.RMW_NEWS, true);
                if (rs.getNumRecords() == 0) {
                    rs.addRecord(s.getBytes(), 0, s.getBytes().length);
                } else {
                    rs.setRecord(1, s.getBytes(), 0, s.getBytes().length);
                }
            } catch (RecordStoreException re) {
                System.out.println("Save news RMS error!");
                return false;
            }
        }
        return true;
    }

    private boolean saveFavorite() {
        if (newsFavorite.size() > 0) {
            String s = null;
            for (int i=0; i<newsFavorite.size(); i++){
                s+= Constant.RMW_NEWS_SPLITCHAR +((News)newsFavorite.elementAt(i)).state + ((News)newsFavorite.elementAt(i)).content;
            }
            if (s.charAt(0)==Constant.RMW_NEWS_SPLITCHAR) {
                s=s.substring(1);
            }            
            try {
                RecordStore rs = RecordStore.openRecordStore(Constant.RMW_NEWS_FAVORITE, true);
                if (rs.getNumRecords() == 0) {
                    rs.addRecord(s.getBytes(), 0, s.getBytes().length);
                } else {
                    rs.setRecord(1, s.getBytes(), 0, s.getBytes().length);
                }
            } catch (RecordStoreException re) {
                System.out.println("Save news RMS error!");
                return false;
            }
        }
        return true;
    }
    
    
    /**
     * Doc Rms chua cac tin tuc
     * @param name: ten rms
     * @return 
     */
//    public News[] readNews(String name){
//        News[] nArray = new News[1];
//        try {
//            RecordStore rs = RecordStore.openRecordStore(name, true);
//            if (rs.getNumRecords() != 0) {
//                /*if (rs.getRecord(1) != null){			
//                    byte status[] = rs.getRecord(1); 
//                    nThemes   = status[0];
//                    nCharSets = status[1];
//                    curScenario = status[2];
//                    nBang = status[3];
//                    nTran = status[4];                       
//                }*/
//                if (rs.getRecord(1) != null){
//                    String sTemp = new String(rs.getRecord(1));
//                    return function.parseNews(sTemp);
//                }
//            }
//            rs.closeRecordStore();
//        } catch(RecordStoreException re) {
//
//        }  
//        return null;
//    }    
}
